package ua.kharkov.khpi.tarasenko.bach.web.command;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import ua.kharkov.khpi.tarasenko.bach.web.command.admin.BanUserCommand;
import ua.kharkov.khpi.tarasenko.bach.web.command.admin.CreateItemCommand;
import ua.kharkov.khpi.tarasenko.bach.web.command.admin.RemoveItemCommand;
import ua.kharkov.khpi.tarasenko.bach.web.command.admin.ShowAllOrdersCommand;
import ua.kharkov.khpi.tarasenko.bach.web.command.admin.ShowCreateItemPageCommand;
import ua.kharkov.khpi.tarasenko.bach.web.command.admin.ShowEditItemViewCommand;
import ua.kharkov.khpi.tarasenko.bach.web.command.admin.ShowUsersListCommand;
import ua.kharkov.khpi.tarasenko.bach.web.command.admin.UnbanUserCommand;
import ua.kharkov.khpi.tarasenko.bach.web.command.admin.UpdateItemCommand;
import ua.kharkov.khpi.tarasenko.bach.web.command.any.AuthenticationCommand;
import ua.kharkov.khpi.tarasenko.bach.web.command.any.ErrorProcessingCommand;
import ua.kharkov.khpi.tarasenko.bach.web.command.any.NoSuchCommand;
import ua.kharkov.khpi.tarasenko.bach.web.command.common.LogOutCommand;
import ua.kharkov.khpi.tarasenko.bach.web.command.common.ShowItemsListCommand;
import ua.kharkov.khpi.tarasenko.bach.web.command.common.ShowSettingsCommand;
import ua.kharkov.khpi.tarasenko.bach.web.command.common.UpdateSettingsCommand;
import ua.kharkov.khpi.tarasenko.bach.web.command.user.AddToCartCommand;
import ua.kharkov.khpi.tarasenko.bach.web.command.user.ClearShoppingCart;
import ua.kharkov.khpi.tarasenko.bach.web.command.user.PayForOrderCommand;
import ua.kharkov.khpi.tarasenko.bach.web.command.user.PlaceOrderCommand;
import ua.kharkov.khpi.tarasenko.bach.web.command.user.ShowItemDescriptionCommand;
import ua.kharkov.khpi.tarasenko.bach.web.command.user.ShowOrdersListCommand;
import ua.kharkov.khpi.tarasenko.bach.web.command.user.ShowShoppingCartCommand;

/**
 * Manager for commands. Provides access to commands using their's names.
 * 
 * @author oleksandr
 * 
 */
public class CommandManager {
	private static final Logger log = Logger.getLogger(CommandManager.class);

	public static final String COMMAND_AUTHENTICATE = "authenticate";
	public static final String COMMAND_LOG_OUT = "log_out";
	public static final String COMMAND_ERROR_PROCESSING = "error_processing";
	public static final String COMMAND_SHOW_ITEM_LIST = "show_item_list";
	public static final String COMMAND_SHOW_ITEM = "show_item";
	public static final String COMMAND_ADD_TO_CART = "add_to_cart";
	public static final String COMMAND_SHOW_SHOPPING_CART = "show_shopping_cart";
	public static final String COMMAND_UPDATE_SETTINGS = "update_settings";
	public static final String COMMAND_SHOW_SETTINGS = "show_settings";
	public static final String COMMAND_PLACE_ORDER = "place_order";
	public static final String COMMAND_CLEAR_SHOPPING_CART = "clear_shopping_cart";
	public static final String COMMAND_SHOW_ORDERS_LIST = "show_orders_list";
	public static final String COMMAND_PAY_FOR_ORDER = "pay_for_order";
	public static final String COMMAND_SHOW_USERS_LIST = "show_users_list";
	public static final String COMMAND_REMOVE_ITEM = "remove_item";
	public static final String COMMAND_EDIT_ITEM = "edit_item";
	public static final String COMMAND_UPDATE_ITEM = "update_item";
	public static final String COMMAND_CREATE_ITEM = "create_item";
	public static final String COMMAND_SHOW_CREATE_ITEM_PAGE = "show_create_item_page";
	public static final String COMMAND_BAN_USER = "ban_user";
	public static final String COMMAND_UNBAN_USER = "unban_user";
	public static final String COMMAND_NO_SUCH_COMMAND = "no_such_command";
	public static final String COMMAND_SHOW_ALL_ORDERS = "show_all_orders";
	private static final Map<String, Command> commands = new HashMap<String, Command>();

	static {
		commands.put(COMMAND_AUTHENTICATE, new AuthenticationCommand());
		commands.put(COMMAND_LOG_OUT, new LogOutCommand());
		commands.put(COMMAND_ERROR_PROCESSING, new ErrorProcessingCommand());
		commands.put(COMMAND_SHOW_ITEM_LIST, new ShowItemsListCommand());
		commands.put(COMMAND_SHOW_ITEM, new ShowItemDescriptionCommand());
		commands.put(COMMAND_ADD_TO_CART, new AddToCartCommand());
		commands.put(COMMAND_SHOW_SHOPPING_CART, new ShowShoppingCartCommand());
		commands.put(COMMAND_SHOW_SETTINGS, new ShowSettingsCommand());
		commands.put(COMMAND_UPDATE_SETTINGS, new UpdateSettingsCommand());
		commands.put(COMMAND_PLACE_ORDER, new PlaceOrderCommand());
		commands.put(COMMAND_CLEAR_SHOPPING_CART, new ClearShoppingCart());
		commands.put(COMMAND_SHOW_ORDERS_LIST, new ShowOrdersListCommand());
		commands.put(COMMAND_PAY_FOR_ORDER, new PayForOrderCommand());
		commands.put(COMMAND_SHOW_USERS_LIST, new ShowUsersListCommand());
		commands.put(COMMAND_REMOVE_ITEM, new RemoveItemCommand());
		commands.put(COMMAND_EDIT_ITEM, new ShowEditItemViewCommand());
		commands.put(COMMAND_UPDATE_ITEM, new UpdateItemCommand());
		commands.put(COMMAND_CREATE_ITEM, new CreateItemCommand());
		commands.put(COMMAND_SHOW_CREATE_ITEM_PAGE,
				new ShowCreateItemPageCommand());
		commands.put(COMMAND_BAN_USER, new BanUserCommand());
		commands.put(COMMAND_UNBAN_USER, new UnbanUserCommand());
		commands.put(COMMAND_NO_SUCH_COMMAND, new NoSuchCommand());
		commands.put(COMMAND_SHOW_ALL_ORDERS, new ShowAllOrdersCommand());
	}

	public static Command getCommand(String commandName) {
		if ((commandName == null) || (!commands.containsKey(commandName))) {
			if (log.isDebugEnabled()) {
				log.debug("Can't find command " + commandName);
			}
			return commands.get(COMMAND_NO_SUCH_COMMAND);
		}
		return commands.get(commandName);
	}
}
